<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Team_model extends CI_Model
{
    public function __construct() 
    { 
        parent::__construct(); 
    }
    
    public function get_teams()
    {
        $wynik = $this->db
                ->order_by('comp_name')
                ->order_by('league_name')
                ->order_by('team_name')
                ->get('view_teams');
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $w)
            {
                $tab['wynik'][$w['team_id']]['name'] = $w['team_name'];
                $tab['wynik'][$w['team_id']]['code'] = $w['team_code'];
                $tab['wynik'][$w['team_id']]['logo'] = $w['team_logo'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['name'] = $w['comp_name'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['code'] = $w['comp_code'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['city'] = $w['city_name'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['ligi'][$w['league_id']]['name'] = $w['league_name'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['ligi'][$w['league_id']]['code'] = $w['league_code'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['ligi'][$w['league_id']]['sezon']= $w['tl_season'];
            }
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    public function get_select_teams($extra=0, $lid='')
    {
        if(empty($lid))
            $wynik = $this->db
                ->select('team_id, team_code, comp_code')
                ->order_by('team_code')
                ->get('view_teams');
        else
            $wynik = $this->db
                ->select('team_id, team_code, comp_code')
                ->order_by('team_code')
                ->get_where('view_teams',array('league_id'=>$lid));
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            $tab['wynik'][0] = '';
            foreach($wynik->result_array() as $w)
                $tab['wynik'][$w['team_id']] = $w['team_code'].($extra == 0 ? '' : ' ('.$w['comp_code'].')');
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    public function get_teams_by_comp($comp_id = NULL)
    {
        $this->db
                ->select('team_id, team_code, comp_id, comp_code, city_name, league_id, league_code')
                ->order_by('comp_code')
                ->order_by('league_code')
                ->order_by('team_code');
        $wynik = (is_null($comp_id) ? $this->db->get('view_teams') : $this->db->get_where('view_teams',array('comp_id'=>$comp_id)));
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $w)
            {
                $tab['wynik'][$w['comp_id']]['code'] = $w['comp_code'];
                $tab['wynik'][$w['comp_id']]['city'] = $w['city_name'];
                $tab['wynik'][$w['comp_id']]['ligi'][$w['league_id']]['code'] = $w['league_code'];
                $tab['wynik'][$w['comp_id']]['ligi'][$w['league_id']]['teams'][$w['team_id']] = $w['team_code'];
            }
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    public function get_teams_by_league($league_id = NULL)
    {
        $this->db
                ->select('team_id, team_code, comp_id, comp_code, city_name, league_id, league_code')
                ->order_by('comp_code')
                ->order_by('league_code')
                ->order_by('team_code');
        $wynik = (is_null($comp_id) ? $this->db->get('view_teams') : $this->db->get_where('view_teams',array('comp_id'=>$comp_id)));
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $w)
            {
                $tab['wynik'][$w['league_id']]['code'] = $w['league_code'];
                $tab['wynik'][$w['league_id']]['teams'][$w['team_id']] = $w['team_code'];
            }
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    /*
     * pobiera drużyny wg ID rozgrywek ($cid) i/lub ID ligi ($lid)
     * 
     * @param int $cid  ID rozgrywek
     * @param int $lid  ID ligi
     * 
     * @return string
     */
    public function get_json_teams($cid = '', $lid = '')
    {
        $this->db
                ->select('team_id, team_code')
                ->order_by('team_name','asc');
        if(empty($cid) && empty($lid))
            $wynik = $this->db->get('view_teams');
        elseif(!empty($cid) && empty($lid))
            $wynik = $this->db->get_where('view_teams',array('comp_id'=>$cid));
        else
            $wynik = $this->db->get_where('view_teams',array('league_id'=>$lid));
        if($wynik->num_rows()>0)
        {
            foreach($wynik->result_array() as $w)
                $tab[$w['team_id']] = $w['team_code'];
            return json_encode($tab);
        }
        else
            return FALSE;
    }
    
    public function insert_team($team)
    {
        $wynik = $this->db->insert('ut_team',$team);
        if($this->db->affected_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
    
    public function insert_team_league($tab)
    {
        $wynik = $this->db->insert('ut_teamleague',$tab);
        if($this->db->affected_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
    
    public function get_leagues()
    {
        $wynik = $this->db->select('league_id, league_code, comp_code')->order_by('comp_code')->order_by('league_code')->get('view_leagues');
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $w)
                $tab['wynik'][$w['league_id']] = $w['comp_code'].' >> '.$w['league_code'];
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    public function get_positions()
    {
        $wynik = $this->db->get('ut_positions');
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $w)
            {
                $tab['wynik'][$w['pos_id']] = $w['pos_code'];
            }
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    public function get_team_info($id)
    {
        $wynik = $this->db->get_where('view_teams',array('team_id'=>$id));
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $w)
            {
                $tab['wynik'][$w['team_id']]['name'] = $w['team_name'];
                $tab['wynik'][$w['team_id']]['code'] = $w['team_code'];
                $tab['wynik'][$w['team_id']]['logo'] = $w['team_logo'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['name'] = $w['comp_name'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['code'] = $w['comp_code'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['city'] = $w['city_name'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['ligi'][$w['league_id']]['name'] = $w['league_name'];
                $tab['wynik'][$w['team_id']]['comps'][$w['comp_id']]['ligi'][$w['league_id']]['code'] = $w['league_code'];
            }
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    public function get_squad($id)
    {
        $wynik = $this->db
                ->select('user_id,
                    user_fname,
                    user_lname, 
                    user_image,
                    pos_id,
                    pos_name,
                    pos_code,
                    pp_number,
                    pt_joined')
                ->order_by('user_lname')
                ->order_by('user_fname')
                ->order_by('pos_code')
                ->get_where('view_users',array('team_id'=>$id));
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $w)
            {
                $tab['wynik'][$w['user_id']]['fname'] = $w['user_fname'];
                $tab['wynik'][$w['user_id']]['lname'] = $w['user_lname'];
                $tab['wynik'][$w['user_id']]['image'] = $w['user_image'];
                $tab['wynik'][$w['user_id']]['number']= $w['pp_number'];
                $tab['wynik'][$w['user_id']]['join']  = $w['pt_joined'];
                $tab['wynik'][$w['user_id']]['posit'][$w['pos_id']]['name'] = $w['pos_name'];
                $tab['wynik'][$w['user_id']]['posit'][$w['pos_id']]['code'] = $w['pos_code'];
            }
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
    
    public function insert_user($tab)
    {
        $wynik = $this->db->insert('ut_users',$tab);
        if($this->db->affected_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
    
    public function insert_player_position($tab)
    {
        $wynik = $this->db->insert('ut_playpos',$tab);
        if($this->db->affected_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
    
    public function insert_player_team($user,$team,$joined)
    {
        $wynik = $this->db->insert('ut_playteam',array('pt_user'=>$user,'pt_team'=>$team,'pt_joined'=>$joined));
        if($this->db->affected_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
}